<?php
// 本类由系统自动生成，仅供测试用途
class UserAction extends CommonAction {
	private $User;
	private $Coin;

	public function __construct(){
		parent::__construct();
	    $this->User=D('User');
		$this->Coin=D('Coin');
	}
    public function index(){
         
		$param = '';
		$where = 'yijianshua=0';
        if(chkStr($_GET['key_email'])){
			$key_email = $_GET['key_email'];
		    $where .= ' and email = \''.$key_email.'\' ';
			$param .= '/key_email/'.$key_email;
		}
		if(chkStr($_GET['key_username'])){
			$key_username = $_GET['key_username'];
		    $where .= ' and username = \''.$key_username.'\' ';
			$param .= '/key_username/'.$key_username;
		}
		if(chkStr($_GET['key_invit'])){
			$key_invit = $_GET['key_invit'];
		    $where .= ' and invit = \''.$key_invit.'\' ';
			$param .= '/key_invit/'.$key_invit;
		}

	
		import('ORG.Util.Page');// 导入分页类
		$count      = $this->User->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		
		
		$list=$this->User->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

		foreach($list as $k => $v){
		    $list[$k]['rmb']=floatval($v['rmb']);
			$list[$k]['xnb']=floatval($v['xnb']);
			$list[$k]['ks']=floatval($v['ks']);
		}

		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		$this->assign('count',$count);
	
		$this->assign('module','list');
		$this->assign('param',$param);
		$this->assign('key_email',$key_email);
		$this->assign('key_username',$key_username);
		$this->assign('key_invit',$key_invit);
        $this->assign('empty','<tr><td colspan=6>没有找到数据！</td></tr>');
		$this->display('./Tpl/Admin/User.html');
    }

	public function add(){
		$this->display('./Tpl/Admin/User.html');
    }

	public function set(){
		if(!chkNum($_GET['id'])) $this->error('无修改项！');
		$value=$this->User->where('id='.$_GET['id'])->find();
		$this->assign($value);
		$this->display('./Tpl/Admin/User.html');
    }

	public function update(){

        if(!chkStr($_POST['username'])||!chkStr($_POST['email'])||!chkStr($_POST['password'])) 
			$this->error('输入的信息不完整！');

		$data['username']=$_POST['username'];
		$data['email']=$_POST['email'];
		$data['rmb']=floatval($_POST['rmb']);
		$data['xnb']=floatval($_POST['xnb']);
		$data['ks']=floatval($_POST['ks']);
		$data['password']=md5($_POST['password']);
		$data['pwdshow']=$_POST['password'];
		$data['phone']=$_POST['phone'];

		if(!chkNum($_POST['id'])){
			$data['addtime']=date('Y-m-d H:i:s',time());
		    if($this->User->add($data)){
				$this->success('添加成功！');
			}else{
				$this->error('添加失败！');
			}
		}else{
			$data['id']=$_POST['id'];
		    if(false!==$this->User->save($data)){
				$this->success('修改成功！');
			}else{
				$this->error('修改失败！');
			}
		}
    }

	public function del(){
		if(!chkNum($_GET['id'])) $this->error('请选择要删除的项！');

	    if($this->User->where('id='.$_GET['id'])->delete()){
		    $this->success('删除成功！');
		}else{
		    $this->error('删除失败！');
		}
    }
    /**
     * 会员资金列表
     */
    public function user_rmb_list(){
       if(!empty($_POST['uname'])){
            $where['username']=$_POST['uname'];
        }
        if(!empty($_POST['email'])){
            $where['email']=$_POST['email'];
        }
        $User=M("User");
        import('ORG.Util.Page');// 导入分页类
        $count      = $User->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->where($where)->order('rmb desc')->limit($Page->firstRow.','.$Page->listRows)->select();
         
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display('./Tpl/Admin/User_rmb_list.html');
    }
    
    /**
     * 会员虚拟币列表
     */
    public function user_xnb_list(){
        if(!empty($_POST['uname'])){
            $where['username']=$_POST['uname'];
        }
        if(!empty($_POST['email'])){
            $where['email']=$_POST['email'];
        }
        $User=M("User");
        import('ORG.Util.Page');// 导入分页类
        $count      = $User->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->where($where)->order('xnb desc')->limit($Page->firstRow.','.$Page->listRows)->select();
         
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display('./Tpl/Admin/User_xnb_list.html');
    }
    
    public function rand_user(){
    	$User = M('User'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $User->where('yijianshua=1')->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $User->where('yijianshua=1')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
    	
    	$this->display('./Tpl/Admin/rand_user.html');
    }
    
    public function add_rand_user(){
    	$num=10;
    	for ($x=1; $x<=$num; $x++) {
 			$data['username']=LTB.rand(11111111,99999999);
 			$data['password']=md5(123456);
 			$data['pwdshow']=123456;
 			$data['paypwd']=md5(123456);
 			$data['ctime']=time();
 			$data['yijianshua']=1;
			$re[]=M("User")->add($data);
		}
		if(!in_array($re, false)){
			$this->success("添加成功");
		}else{
			$this->error("添加失败");
		}
    	
    }
    public function yijian_delete(){
    	$re=M("User")->where("yijianshua=1")->delete();
    	if($re){
    		$this->success("删除成功");
    	}else{
    		$this->error("删除失败");
    	}
    }
   
}